/**
  ******************************************************************************
  * @file   :   .c
  * @version:
  * @author :
  * @brief  :
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright(c) . BLUEX Microelectronics.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */

/* includes ------------------------------------------------------------------*/

#include "bx_kernel.h"
#include "user_service_adc.h"
#include "bx_drv_adc.h"

#include "bx_shell.h"
/* private define ------------------------------------------------------------*/

/* private typedef -----------------------------------------------------------*/
struct us_adc_service {
    s32 id;
    void * handle;
    
};
/* private variables ---------------------------------------------------------*/
static struct us_adc_service adc_svc = {0};
/* exported variables --------------------------------------------------------*/
extern u32 bat_volt;
extern u32 channel;
/* private macros ------------------------------------------------------------*/
#define GET_ADC_SERVICE_BY_ID( p_svc, svc_id )                  \
do{                                                             \
    if( ( svc_id ) == adc_svc.id ) {                            \
        p_svc = &adc_svc;                                       \
    } else {                                                    \
        return BX_ERR_NOTSUP;                                   \
    }                                                           \
}while(0)

/*============================= private function =============================*/
static bx_err_t adc_property_get(s32 svc_id, u32 property, u32 param0, u32 param1 );

/** ---------------------------------------------------------------------------
 * @brief   :
 * @note    :
 * @param   :
 * @retval  :
-----------------------------------------------------------------------------*/
static bx_err_t adc_msg_handle(s32 svc_id, u32 msg, u32 param0, u32 param1 )
{
    struct us_adc_service * p_svc;
    GET_ADC_SERVICE_BY_ID( p_svc, svc_id );

	bx_err_t bx_err;
	
    switch( msg ) {
        case BXM_OPEN:
			bx_err = bx_drv_adc_open(p_svc->handle);
            break;
        
        case BXM_CLOSE:
			bx_err = bx_drv_adc_close(p_svc->handle);
            break;
		

		case BXM_ADC_VOLT:
			bx_err = adc_property_get(svc_id,BXP_ADC_VOLT,param0,param1);			
			break;


		case BXM_ADC_BATTERY:
			bx_err = adc_property_get(svc_id,BXP_ADC_BATTERY,param0,param1);
			
			break;
		
		case BXM_ADC_TEMPERATURE:
			bx_err = adc_property_get(svc_id,BXP_ADC_TEMPERATURE,param0,param1);
			
			break;

        default:
            return BX_ERR_NOTSUP;
    }
	return bx_err;
}

/** ---------------------------------------------------------------------------
 * @brief   :
 * @note    :
 * @param   :
 * @retval  :
-----------------------------------------------------------------------------*/
static bx_err_t adc_property_set(s32 svc_id, u32 property, u32 param0, u32 param1 )
{
//    struct us_adc_service * p_svc;
//    GET_ADC_SERVICE_BY_ID( p_svc, svc_id );
    
    switch( property ) {
        default:
            return BX_ERR_NOTSUP;
    }
}

/** ---------------------------------------------------------------------------
 * @brief   :
 * @note    :
 * @param   :
 * @retval  :
-----------------------------------------------------------------------------*/
static bx_err_t adc_property_get(s32 svc_id, u32 property, u32 param0, u32 param1 )
{
    struct us_adc_service * p_svc;
    GET_ADC_SERVICE_BY_ID( p_svc, svc_id );

	bx_err_t bx_err;

    switch( property ) {			
        
        case BXP_ADC_VOLT:
            bx_err = bx_drv_adc_get_volt(p_svc->handle,param0,&param1);
			bxsh_logln("BXP_ADC_VOLT:\nbx_err = %d channel = %d   volt_mv = %d ", bx_err,param0,param1);
			break;
			
        
        case BXP_ADC_BATTERY:
            bx_err = bx_drv_adc_get_battery(p_svc->handle, &param0);
			bxsh_logln("BXP_ADC_BATTERY:\nbx_err = %d   bat_volt = %d ",bx_err, param0);
		   break;
			
        
        case BXP_ADC_TEMPERATURE:
            bx_err = bx_drv_adc_get_chip_temperature(p_svc->handle,&param0);
			bxsh_logln("BXP_ADC_TEMPERATURE:\nbx_err = %d temp = %d", bx_err,(param0/10));
			break;
        
        default:
			   return BX_ERR_NOTSUP;
         
    }
    return BX_OK;
}



/*========================= end of private function ==========================*/


/*============================= exported function ============================*/

/** ---------------------------------------------------------------------------
 * @brief   :
 * @note    :
 * @param   :
 * @retval  :
-----------------------------------------------------------------------------*/
bool us_adc_register( void )
{
    struct bx_service svc;
    svc.name = "adc service";
    svc.msg_handle_func = adc_msg_handle;
    svc.prop_get_func = adc_property_get;
    svc.prop_set_func = adc_property_set;
    adc_svc.id = bx_register( &svc );
    if( adc_svc.id == -1 ) {
        return false;
    }

	adc_svc.handle = BX_ADC;
    return true;
}
/** ---------------------------------------------------------------------------
 * @brief   :
 * @note    :
 * @param   :
 * @retval  :
-----------------------------------------------------------------------------*/
s32 us_adc_id( void )
{
    return adc_svc.id;
}

/*========================= end of exported function =========================*/


/*============================= import function ==============================*/

/*=========================== end of import function =========================*/


/*============================ interrupt function ============================*/

/*========================= end of interrupt function ========================*/


/******************** (C) COPYRIGHT BLUEX **********************END OF FILE****/



